﻿

var ListHelper = {

    GetList: function () {
        var context = SP.ClientContext.get_current();

        //No need to use SP.RequestExecutor.js for cross domain calls to host web in SP Hosted web
        /* var factory = new SP.ProxyWebRequestExecutorFactory(appWebUrl);
        context.set_webRequestExecutorFactory(factory); */

        //var hostContext = new SP.AppContextSite(context, hostWebUrl);

        var list = context.get_web().get_lists().getByTitle("Worksheets");
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml("<View><RowLimit>10</RowLimit></View>");

        listItems = list.getItems(camlQuery);

        context.load(listItems, "Include(Id, Title, Url)");
        context.executeQueryAsync(ListHelper.ListItemsLoaded, ListHelper.ListItemsFailed);
    },

    ListItemsLoaded: function (sender, args) {
        var enumerator = listItems.getEnumerator();

        //Clear tabs
        var tabs = $('#worksheets-items');
        //tabs.html('');
        while (enumerator.moveNext()) {
            var currentItem = enumerator.get_current();
            var id = currentItem.get_id();
            var title = currentItem.get_item("Title");
            var url = currentItem.get_item("Url");
            console.log('title:' + title + ' url:' + url);
            tabs.append('<a href="' + url + '?IsDlg=1&IsSPExplorer=1&output=embed" class="btn btn-info we-link">' + title + '</a>');
            AddNewRowUI(id, title, url);
        }
        tabs.append('<a href="#" class="btn btn-primary" data-toggle="modal" data-target="#worksheets-manage">+</a>');

        $('#worksheets-items a.we-link').first().click();
    },

    ListItemsFailed: function (sender, args) {
        alert(args.get_message());
    },

    Add: function (title, url) {

        var context = SP.ClientContext.get_current();

        var list = context.get_web().get_lists().getByTitle("Worksheets");

        var itemCreateInfo = new SP.ListItemCreationInformation();
        var oListItem = list.addItem(itemCreateInfo);

        oListItem.set_item('Title', title);
        oListItem.set_item('Url', url);

        oListItem.update();

        context.load(oListItem);

        context.executeQueryAsync(ListHelper.AddSuccessed, ListHelper.AddFailed);
    },

    AddSuccessed: function (sender, args) {
        console.log('Item created: ' + oListItem.get_id());
    },

    AddFailed: function (sender, args) {
        alert(args.get_message());
    },

    Delete: function (id) {
        var context = SP.ClientContext.get_current();

        var list = context.get_web().get_lists().getByTitle("Worksheets");

        var oListItem = list.getItemById(id);

        oListItem.deleteObject();

        context.executeQueryAsync(ListHelper.DeleteSuccessed, ListHelper.DeleteFailed);
    },

    DeleteSuccessed: function (sender, args) {

    },

    DeleteFailed: function (sender, args) {
        alert(args.get_message());
    }
};
